package Model.Core.Impl;

import Factory.Entities.ObjectFactory;
import Factory.Entities.StateFactory;
import Factory.Tip.TipsFactory;
import Model.Core.ICommunicatorEntity;
import Model.Core.Tip.Tip;
import Model.Core.Tip.TipMoment;
import Model.Core.Tip.TipType;
import Model.Diagrams.StateMachine;
import Model.Draw.Events.ManifestEntityDoneListener;
import java.util.List;

/**
 * Projeto de TCC: Editor de Diagramas de Comunicação em UML.
 * @author jose.luz
 */
public class ObjectEntity implements ICommunicatorEntity {

    private String label;
    private ObjectFactory objectFactory;
    private List<Tip> tips;
    private StateMachine stateMachine;

    public ObjectEntity() {
        //TODO: get objectFactory from Spring.
        objectFactory = new ObjectFactory();
        tips = TipsFactory.createObjectTips();
        label = "novo objeto";
        stateMachine = StateFactory.createStateMachine(label);
    }

    @Override
    public String getDescription() {
        return label;
    }

    public void setName(String name) {
        label = name;
        stateMachine.setDescription(name);
    }

    @Override
    public void manifestGraphically(Object sourceElement, Object targetElement) {
        objectFactory.createEntityGraphicalManifest(this, sourceElement, targetElement);
    }

    @Override
    public void addGraphicalManifestListener(ManifestEntityDoneListener listener) {
        objectFactory.addListener(listener);
    }

    @Override
    public void removeGraphicalManifestListener(ManifestEntityDoneListener listener) {
        objectFactory.removeListener(listener);
    }

    @Override
    public void select() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public StateMachine getStateMachine() {
        return stateMachine;
    }

    @Override
    public Tip findTipBy(TipType type, TipMoment moment) {
        for (Tip tip : tips) {
            if (tip.getType() == type && tip.getMoment() == moment) {
                return tip;
            }
        }
        return null;
    }

    /*@Override
    public boolean equals(Object obj) {
    if (obj == null) {
    return false;
    }
    if (getClass() != obj.getClass()) {
    return false;
    }
    ObjectEntity other = (ObjectEntity) obj;
    if ((this.label == null) ? (other.label != null) : !this.label.equals(other.label)) {
    return false;
    }
    return true;
    }
    
    @Override
    public int hashCode() {
    int hash = 7;
    hash = 83 * hash + (this.label != null ? this.label.hashCode() : 0);
    return hash;
    }*/
    @Override
    public void setDescription(String description) {
        this.label = description;
        stateMachine.setDescription(description);        
    }
}